<?php
class Model_SaleCategory
{
    public static  function getAllSaleCategory(){
        $db = Core_Global::getDatabase();
        $stmt = $db->prepare('Select SaleCategoryID,SaleCategoryName from salecategory');
        $stmt->execute();
        $result = $stmt->fetchAll();
        return $result;
    }
    public static function getAllSaleCategoryIds()
    {
        $cache = Core_Global::getCache();
        $listIds = $cache->get('SaleCategory:ListIds');

        if (empty($listIds)) {
            $arrData = Business_SaleCategory::getAllId();
            $listIds = array();

            foreach($arrData as $data)
            {
                $listIds[] = $data['SaleCategoryId'];
            }

            $cache->set('SaleCategory:ListIds', $listIds, 604800);
        }

        return $listIds;
    }

    public static function getAll($params)
    {
        $listId = self::getAllSaleCategoryIds();
        $total = count($listId);
        $pages = ceil($total / $params['limit']);
        $start = ($params['page'] - 1) * $params['limit'];
        $end = $start + $params['limit'];
        if ($end > $total) {
            $end = $total;
        }

        $temp = array_splice($listId, $start, $end);

        if (empty($temp))
        {
            return null;
        }

        return self::getAllIds($temp);
    }

    public static function getAllIds($listIds)
    {
        try
        {
            $cache = Core_Global::getCache();
            $cacheKeys = array();

            foreach($listIds as $id)
            {
                $cacheKeys[] = 'SaleCategory:id:' . $id;
            }

            $result = $cache->getMulti($cacheKeys);

            //Miss cache
            if ($result === false || count($result) != count($cacheKeys))
            {
                if ($result === false) $result = array();

                $missKeys = array_diff(array_keys(array_flip($cacheKeys)), array_keys($result));

                array_walk($missKeys, function(&$item){
                    $item = explode(':', $item);
                    $item = $item[count($item) - 1];
                });

                //Get result from database
                $missResult = Business_SaleCategory::getAllIds(implode(',', $missKeys));

                foreach($missResult as $location)
                {
                    $cache->set('SaleCategory:id:' . $location['SaleCategoryID'], $location, 604800);
                }

                $result = array_merge($result, $missResult);
            }

            return $result;
        }
        catch(Zend_Exception $e)
        {
            var_dump($e->getMessage());
        }
    }

    public static function get($params)
    {
        $cache = Core_Global::getCache();
        $row = $cache->get('SaleCategory:' . $params['value']);
        if ($row === false) {
            $row = Business_SaleCategory::get($params);
            $cache->set('SaleCategory:' . $params['value'], $row, 604800);
        }
        return $row;
    }

    public static function create($params)
    {

        $result = Business_SaleCategory::create($params);
        if ($result) {
            $cache = Core_Global::getCache();
            $cache->delete('SaleCategory:ListIds');
        }
        return $result;
    }

    public static function update($params)
    {
        $result = Business_SaleCategory::update($params);
        if ($result) {
            $cache = Core_Global::getCache();
            $cache->delete('SaleCategory:id:' . $params['catId']);
        }
        return $result;
    }

    public static function delete($catId)
    {
        $result = Business_SaleCategory::delete($catId);
        if ($result) {
            $cache = Core_Global::getCache();
            $cache->delete('SaleCategory:id:' . $catId);
            $cache->delete('SaleCategory:ListIds');
        }
        return $result;
    }

    public static function find($params)
    {
        return Business_SaleCategory::find($params);
    }
}